package com.woniu.service.impl;

import com.woniu.mapper.PowerDao;
import com.woniu.mapper.RoleDao;
import com.woniu.mapper.RolepowerDao;
import com.woniu.pojo.*;
import com.woniu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public  class RoleServiceImpl implements RoleService {
    @Autowired
    RoleDao roleDao;
    @Autowired
    RolepowerDao rolepowerDao;
    @Autowired
    PowerDao powerDao;
    //增加角色
    @Override
    public void add(Role role) {
        roleDao.insert(role);
    }
    //删除一个角色
    @Override
    public void deleteById(Integer roleId) {
        roleDao.deleteByPrimaryKey(roleId);
    }
    //删除多个角色
    @Override
    public void deleteByExample(Integer[] roleId) {
        RoleExample roleExample=new RoleExample();
        roleExample.createCriteria().andRoleIdIn(Arrays.asList(roleId));
        roleDao.deleteByExample(roleExample);
    }
    //修改角色
    @Override
    public void updateByExample(Role role) {

        roleDao.updateByPrimaryKeySelective(role);
    }

    @Override
    public List<Role> findAll(String roleName) {
        if(roleName!=null&&!roleName.equals("")) {
            RoleExample example = new RoleExample();
            example.createCriteria().andRoleNameLike("%"+roleName+"%");
            return roleDao.selectByExample(example);
        }else {
            return roleDao.selectByExample(null);
        }
    }

    @Override
    public Role findOne(Integer roleId) {
       return roleDao.selectByPrimaryKey(roleId);

    }

    @Override
    public boolean issame(String roleName) {
        RoleExample roleExample=new RoleExample();
        roleExample.createCriteria().andRoleNameEqualTo(roleName);
        List<Role> roles = roleDao.selectByExample(roleExample);
        if(roles.size()>0){
            return false;
        }
        return true;
    }

    @Override
    public List<Power> getRolePower(Integer roleId) {
        RolepowerExample example=new RolepowerExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<Rolepower> rolepowers = rolepowerDao.selectByExample(example);
        List<Power> tree=new ArrayList<>();
        List<Power> powers = powerDao.selectByExample(null);
        Map<Integer,Power> map=new HashMap<>();
        for(Power p:powers ){
            if(rolepowers!=null&&rolepowers.size()>0){
                for(Rolepower rp:rolepowers){
                    if(p.getPowerId()==rp.getPowerId()){
                        p.setChecked(true);

                        break;
                    }

                }
            }
            map.put(p.getPowerId(),p);
            p.setOpen(true);
        }
        for(Power p:powers){
            if(p.getParentId()==null){
                tree.add(p);
            }else {
                if(map.get(p.getParentId()).getParentId()==null){
                    map.get(p.getParentId()).getChildren().add(p);
                }

            }
        }
        return tree;

    }


    @Override
    public void rolePowerAdd(Integer []rp,Integer roleId) {
        RolepowerExample example=new RolepowerExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        rolepowerDao.deleteByExample(example);
        Rolepower rolepower=new Rolepower();
        rolepower.setRoleId(roleId);
       for(Integer e:rp){
           rolepower.setPowerId(e);
           rolepowerDao.insert(rolepower);
       }

    }


}
